package com.qianfeng.vhly.youshare.adapters;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: vhly[FR]
 * Date: 15/2/4
 * Email: vhly@163.com
 */

/**
 * 通用的图片显示Adapter
 */
public class CommonImagePagerAdapter extends PagerAdapter {

    private List<Integer> imageIds;

    private Context context;

    public CommonImagePagerAdapter(Context context, List<Integer> imageIds) {
        this.imageIds = imageIds;
        this.context = context;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (imageIds != null) {
            ret = imageIds.size();
        }
        return ret;
    }

    @Override
    public boolean isViewFromObject(View view, Object o) {
        return view == o;
    }

    // 创建和销毁 视图


    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView ret = new ImageView(context);

        ret.setScaleType(ImageView.ScaleType.FIT_XY);

        ret.setImageResource(imageIds.get(position));

        container.addView(ret);

        return ret;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (object instanceof ImageView) {
            container.removeView((View) object);
        }
    }
}
